/* definitions of jquery extensions or widgets */

/**
 */
$.fn.reset = function( fn ) {
    return (typeof fn != 'undefined' && fn != null) ? this.bind( "reset", fn ) : this.trigger("reset");
};

// datepicker initialization
$.datepicker.regional['ro'] = {
    closeText: 'Închide',
    prevText: '&laquo; Luna precedentă',
    nextText: 'Luna următoare &raquo;',
    currentText: 'Azi',
    monthNames: ['Ianuarie','Februarie','Martie','Aprilie','Mai','Iunie',
    'Iulie','August','Septembrie','Octombrie','Noiembrie','Decembrie'],
    monthNamesShort: ['Ian', 'Feb', 'Mar', 'Apr', 'Mai', 'Iun',
    'Iul', 'Aug', 'Sep', 'Oct', 'Noi', 'Dec'],
    dayNames: ['Duminică', 'Luni', 'Marţi', 'Miercuri', 'Joi', 'Vineri', 'Sâmbătă'],
    dayNamesShort: ['Dum', 'Lun', 'Mar', 'Mie', 'Joi', 'Vin', 'Sâm'],
    dayNamesMin: ['Du','Lu','Ma','Mi','Jo','Vi','Sâ'],
    weekHeader: 'Săpt',
    dateFormat: 'dd-mm-yy',
    firstDay: 1,
    isRTL: false,
    showMonthAfterYear: false,
    yearSuffix: '',
    showOtherMonths: true,
	selectOtherMonths: true
};
$.datepicker.setDefaults($.datepicker.regional['ro']);

/**
 * Define custom handlers for an ajax rquest
 */
$.ajaxSettings.loader = '#ajax-loader';
$(document).ajaxStart(function(){
    // show the ajax-loader animated image
    $($.ajaxSettings.loader).show();
});
$(document).ajaxStop(function(){
    // show the ajax-loader animated image
    $($.ajaxSettings.loader).hide();
});

/**
 * Overwrite widget constructor: all methods that start with '_init' are automatically executed (at the end of the contructor)
 */
$.Widget.prototype._init = function(){
    // make the widget element also available at $element property
    this.$element = this.element;

    var prefix = '_init';
    for (var method in this) {
        if (method.indexOf(prefix) == 0 && method.length > prefix.length && $.isFunction(this[method])) {
            this[method].call(this);
        }
    }

}

/**
 * Performs an action at the specified document event if the target doesn't match any selector
 *
 * @param string event A jQuery event
 * @param array selectors An array with jQuery selectors
 * @param callback action The callback to be executed
 */
$.documentEvent = function( event, selectors, action )
{
    // if the event is not specified, it defaults to 'click'
    if ( !Type.isFunction(action) )
    {
        action = selectors;
        selectors = event;
        event = 'click';
    }

    // the selectors must be an array
    if ( !Type.isArray(selectors) )
    {
        selectors = [selectors];
    }

    // bind the event to the document
    $(document).bind( event, function( e ){

        // if the target doesn't match any selector, then the action is performed
        var executeAction = true,
            $target = $(e.target);

        $.each( selectors, function(key){
            // find the closest element that match the selector, starting from target
            if ( $target.closest(selectors[key]).length )
            {
                executeAction = false;
                return false;
            }
        })

        if (executeAction)
        {
            // the context of the callback is the target element
            action.call(e.target);
        }
    })
}
